﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BLL
{
    public class ItemManager
    {
        /// <summary>
        ///     通过指定用户ID得到该用户所有分组信息字典
        /// </summary>
        /// <param name="UserId" type="string">
        ///     <para>
        ///         用户ID
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回null , 则表示不存在该用户
        /// </returns>
        public Dictionary<int,String> GetItemByUserId(String UserId)
        {
            DataTable DT_ITEMINFO = Globle.itemService.GetItemByUserId(UserId);
            if (DT_ITEMINFO != null && DT_ITEMINFO.Rows.Count > 0)
            {
                Dictionary<int, String> DIC_ITEMINFO = new Dictionary<int, string>();
                for (int i = 0; i < DT_ITEMINFO.Rows.Count; i++)
                {
                    DIC_ITEMINFO.Add(int.Parse(DT_ITEMINFO.Rows[i]["ItemId"].ToString()), DT_ITEMINFO.Rows[i]["ItemName"].ToString());
                }
                return DIC_ITEMINFO;
            }
            return null;
        }

        /// <summary>
        ///     根据指定分组ID, 更新用户分组名
        /// </summary>
        /// <param name="ItemName">新的分组名</param>
        /// <param name="ItemId">分组ID</param>
        /// <returns></returns>
        public bool UpdateItem(String ItemName, int ItemId)
        {
            return Globle.itemService.UpdateItem(ItemName, ItemId);
        }

        /// <summary>
        ///     添加新的分组到指定用户名分组集合下
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="ItemName">新分组名</param>
        /// <returns>返回-1 , 表示添加失败</returns>
        public int AddItem(String username, String ItemName)
        {
            return this.AddItem(int.Parse(Globle.userManager.GetUserId(username)), ItemName);
        }

        /// <summary>
        ///     添加新的分组到指定用户Id分组集合下
        /// </summary>
        /// <param name="UserId">用户Id</param>
        /// <param name="ItemName">新分组名</param>
        /// <returns>返回-1 , 表示添加失败</returns>
        public int AddItem(int UserId, String ItemName)
        {
            return Globle.itemService.AddItem(UserId, ItemName);
        }

        /// <summary>
        ///     删除指定分组ID删除
        /// </summary>
        /// <param name="ItemId">分组ID</param>
        /// <returns>返回false , 则表示删除失败</returns>
        public bool DeleteItem(String ItemId)
        {
            return DeleteItem(int.Parse(ItemId));
        }

        /// <summary>
        ///     删除指定分组ID删除
        /// </summary>
        /// <param name="ItemId">分组ID</param>
        /// <returns>返回false , 则表示删除失败</returns>
        public bool DeleteItem(int ItemId)
        {
            return Globle.itemService.DeleteItem(ItemId);
        }

         /// <summary>
        ///     通过用户名得到该用户的所有分组ID
        /// </summary>
        /// <param name="UserId">用户名</param>
        /// <returns>返回该用户对应的所有分组ID</returns>
        public int[] GetItemsByUserId(String username)
        {
            return Globle.itemService.GetItemsByUserId(int.Parse(Globle.userManager.GetUserId(username)));
        }

         /// <summary>
        ///     通过用户ID得到该用户的所有分组ID
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <returns>返回该用户对应的所有分组ID</returns>
        public int[] GetItemsByUserId(int UserId)
        {
            return Globle.itemService.GetItemsByUserId(UserId);
        }
    }
}
